
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta charset="utf-8"></meta>
      <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
      <title>13.1.&nbsp;Running Cypher fragments - Chapter&nbsp;13.&nbsp;Cypher Execution</title>
      <link rel="stylesheet" type="text/css" href="../../docbook.css"></link>
      <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans:400,300,400italic,600,300italic"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/codemirror.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/theme/neo.min.css"></link>
      <link rel="stylesheet" type="text/css" href="../../css/chunked-base.css"></link>
      <link rel="stylesheet" type="text/css" href="../../css/extra.css"></link><script src="//code.jquery.com/jquery-1.12.4.js" type="text/javascript"></script><script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js" type="text/javascript"></script><script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/codemirror.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/addon/runmode/runmode.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/mode/cypher/cypher.min.js" type="text/javascript"></script><script src="../../javascript/datatable.js" type="text/javascript"></script><script src="../../javascript/colorize.js" type="text/javascript"></script><script src="../../javascript/tabs-for-chunked.js" type="text/javascript"></script><script src="../../javascript/mp-nav.js" type="text/javascript"></script><script src="../../javascript/versionswitcher.js" type="text/javascript"></script><script src="../../javascript/version.js" type="text/javascript"></script><script src="//s3-eu-west-1.amazonaws.com/alpha.neohq.net/docs/new-manual/assets/search.js" type="text/javascript"></script><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"></meta>
      <link rel="prev" href="../" title="Chapter&nbsp;13.&nbsp;Cypher Execution"></link>
      <link rel="next" href="../cypher-timeboxed/" title="13.1.2.&nbsp;Running a cypher statement timeboxed"></link>
      <link rel="shortcut icon" href="https://neo4j.com/wp-content/themes/neo4jweb/favicon.ico"></link><script>
        $(document).ready(function() {
          CodeMirror.colorize();
          tabTheSource($('body'));
          var $header = $('header').first();
          $header.prepend(
            $('<a href="" id="logo"><img src="https://neo4j.com/wp-content/themes/neo4jweb/assets/images/neo4j-logo-2015.png" alt="Neo4j Logo"></img></a>')
          );
          var $sidebar = $('<div id="sidebar-wrapper"></div>');
          $.get('toc.html', function (d){
            $(d).appendTo($sidebar);
            highlightToc();
            highlightLibraryHeader();
          });
          $sidebar.insertAfter($('header').first());
        });
        </script></head>
   <body>
      <header>
         <div class="searchbox">
            <form id="search-form" class="search" name="search-form" role="search"><input id="search-form-input" name="q" title="search" type="search" lang="en" placeholder="Search Neo4j docs..." aria-label="Search Neo4j documentation" max-length="128" required="required"></input><input id="search-form-button" type="submit" value="Search"></input></form>
         </div>
         <ul class="documentation-library">
            <li><a href="https://neo4j.com/docs/operations-manual/current">Operations Manual</a></li>
            <li><a href="https://neo4j.com/docs/developer-manual/current/">Developer Manual</a></li>
            <li><a href="https://neo4j.com/docs/ogm-manual/current/">OGM Manual</a></li>
            <li><a href="https://neo4j.com/docs/graph-algorithms/current/">Graph Algorithms</a></li>
            <li><a href="https://neo4j-contrib.github.io/neo4j-apoc-procedures/3.4/">APOC</a></li>
            <li><a href="https://neo4j.com/docs/java-reference/current/">Java Reference</a></li>
         </ul>
         <nav id="header-nav"><span class="nav-previous"><a accesskey="p" href="../"><span class="fa fa-long-arrow-left" aria-hidden="true"></span>Cypher Execution</a></span><span class="nav-current">
               <p class="nav-title hidden">13.1.&nbsp;Running Cypher fragments</p></span><span class="nav-next"><a accesskey="n" href="../cypher-timeboxed/">Running a cypher statement timeboxed<span class="fa fa-long-arrow-right" aria-hidden="true"></span></a></span></nav>
      </header>
      <div id="search-results" class="hidden"></div>
      <section class="section" id="running-cypher">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title" style="clear: both"><a class="anchor" href="#running-cypher"></a>13.1.&nbsp;Running Cypher fragments
                  </h2>
               </div>
            </div>
         </div>
         <p>We can use Cypher as safe, graph-aware, partially compiled scripting language within APOC.</p>
         <div class="informaltable">
            <div class="table" id="d0e16256">
               <table class="informaltable" border="1">
                  <colgroup>
                     <col class="col_1"></col>
                     <col class="col_2"></col>
                     <col class="col_3"></col>
                  </colgroup>
                  <thead>
                     <tr>
                        <th style="text-align: left; vertical-align: top; ">type</th>
                        <th style="text-align: left; vertical-align: top; ">qualified name</th>
                        <th style="text-align: left; vertical-align: top; ">description</th>
                     </tr>
                  </thead>
                  <tbody>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.runTimeboxed</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.runTimeboxed('cypherStatement',{params}, timeout) - abort kernelTransaction after timeout ms if not finished</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.run</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.run(fragment, params) yield value - executes reading fragment with the given parameters</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.runFile</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.runFile(file or url,[{statistics:true,timeout:10,parameters:{}}]) - runs each statement in the file, all semicolon
                              separated - currently no schema operations
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.runFiles</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.runFiles([files or urls],[{statistics:true,timeout:10,parameters:{}}])) - runs each statement in the files, all
                              semicolon separated
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.runSchemaFile</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.runSchemaFile(file or url,[{statistics:true,timeout:10}]) - allows only schema operations, runs each schema statement
                              in the file, all semicolon separated
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.runSchemaFiles</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.runSchemaFiles([files or urls],{statistics:true,timeout:10}) - allows only schema operations, runs each schema
                              statement in the files, all semicolon separated
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.runMany</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.runMany('cypher;\nstatements;',{params},[{statistics:true,timeout:10}]) - runs each semicolon separated statement
                              and returns summary - currently no schema operations
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.parallel</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">&nbsp;</td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.mapParallel</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.mapParallel(fragment, params, list-to-parallelize) yield value - executes fragment in parallel batches with the
                              list segments being assigned to _
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.mapParallel2</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.mapParallel2(fragment, params, list-to-parallelize) yield value - executes fragment in parallel batches with the
                              list segments being assigned to _
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.parallel2</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">&nbsp;</td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.doIt</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.doIt(fragment, params) yield value - executes writing fragment with the given parameters</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.when</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.when(condition, ifQuery, elseQuery:'', params:{}) yield value - based on the conditional, executes read-only ifQuery
                              or elseQuery with the given parameters
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.do.when</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.do.when(condition, ifQuery, elseQuery:'', params:{}) yield value - based on the conditional, executes writing ifQuery
                              or elseQuery with the given parameters
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.case</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.case([condition, query, condition, query, &#8230;&#8203;], elseQuery:'', params:{}) yield value - given a list of conditional / read-only
                              query pairs, executes the query associated with the first conditional evaluating to true (or the else query if none are true)
                              with the given parameters
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>procedure</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.do.case</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.do.case([condition, query, condition, query, &#8230;&#8203;], elseQuery:'', params:{}) yield value - given a list of conditional
                              / writing query pairs, executes the query associated with the first conditional evaluating to true (or the else query if none
                              are true) with the given parameters
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>function</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.runFirstColumn</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>use either apoc.cypher.runFirstColumnMany for a list return or apoc.cypher.runFirstColumnSingle for returning the first row
                              of the first column
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>function</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.runFirstColumnMany</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.runFirstColumnMany(statement, params) - executes statement with given parameters, returns first column only collected
                              into a list, params are available as identifiers
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>function</p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.cypher.runFirstColumnSingle</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>apoc.cypher.runFirstColumnSingle(statement, params) - executes statement with given parameters, returns first element of the
                              first column only, params are available as identifiers
                           </p>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
         </div>
         <section class="section" id="_example_fast_node_counts_by_label">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a class="anchor" href="#_example_fast_node_counts_by_label"></a>13.1.1.&nbsp;Example: Fast Node-Counts by Label
                     </h3>
                  </div>
               </div>
            </div>
            <p>Counts by label are quickly provided by the counts-store, but only if they are the the single thing in the query, like</p><pre class="programlisting highlight"><code data-lang="cypher">MATCH (:Person) RETURN count(*);</code></pre><p>It also works to combine several with <code class="literal">UNION ALL</code>, but not via <code class="literal">WITH</code></p>
            <p><span class="formalpara-title">Doesn&#8217;t work.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">MATCH (:Person) WITH count(*) as people
MATCH (:Movie) RETURN people, count(*) as movies;</code></pre><p>
               
            </p>
            <p><span class="formalpara-title">Works.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">MATCH (:Person) RETURN count(*)
UNION ALL
MATCH (:Movie) RETURN count(*);</code></pre><p>
               
            </p>
            <p>But with <code class="literal">apoc.cypher.run</code> we can construct the statement and run each of them individually, so it completes in a few ms.
            </p><pre class="programlisting highlight"><code data-lang="cypher">call db.labels() yield label
call apoc.cypher.run("match (:`"+label+"`) return count(*) as count", null) yield value
return label, value.count as count</code></pre><p>You can use a similar approach to get the property-keys per label:</p><pre class="programlisting highlight"><code data-lang="cypher">CALL db.labels() yield label
call apoc.cypher.run("MATCH (n:`"+label+"`) RETURN keys(n) as keys LIMIT 1",null) yield value
RETURN label, value.keys as keys</code></pre></section>
         <section class="section" id="_run_multiple_statements">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a class="anchor" href="#_run_multiple_statements"></a>13.1.3.&nbsp;Run multiple Statements
                     </h3>
                  </div>
               </div>
            </div>
            <p>This procedure runs each semicolon separated statement and returns summary - currently no schema operations.</p><pre class="screen highlight"><code>apoc.cypher.runMany('cypher;\nstatements;',{params},[{statistics:true,timeout:10}])</code></pre></section>
      </section>
      <footer><script type="text/javascript">
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
          //Allow Linker
          ga('create', 'UA-1192232-34','auto', {'allowLinker': true});
          ga('send', 'pageview');
          // Load the plugin.
          ga('require', 'linker');
          // Define which domains to autoLink.
          ga('linker:autoLink', ['neo4j.org','neo4j.com','neotechnology.com','graphdatabases.com','graphconnect.com']);
        </script><script type="text/javascript">
          document.write(unescape("%3Cscript src='//munchkin.marketo.net/munchkin.js' type='text/javascript'%3E%3C/script%3E"));
        </script><script>Munchkin.init('773-GON-065');</script></footer>
   </body>
</html>